package com.netik.main;
import com.netik.adapters.*;
import com.netik.getdata.*;

import java.util.ArrayList;



import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class EnquiryExecute extends DashboardActivity{
    /** Called when the activity is first created. */
	public DatabaseAdapternew mDbHelper;
	private DatabaseAdapternew mDbHelper_sort;
	/*private String[] inName;
    private String[] inDesc;
    private String[] inAdd;
    private int[] inPrice;
    private String[] allDesc;
    private String[] allAddress;
    private int[] allPrice;
    */
	private String[] sort_Name;
	private String[] sort_Id;
	private String[] sort_Val;
	
	private String[] allName;
	private String[] allId;
	private String[] allVal;
	
	private String[] resName;
	private String[] resId;
	private String[] resVal;
	
	private String[] Name;
	private String[] Price;
	private String[] Value;
	
    private int i;
    private Cursor cursor,s_cursor;
    private Intent intent;
    private ImageButton SearchItem;
    private ImageButton SortAsc;
    private ImageButton SortDesc;
    private ImageButton publish;
    private TextView SearchText;
    private String key;
    GetDatanew gd = new GetDatanew();
    private String[] dt =new String[1000];
    private String[] tt = new String[1000];
    private String[] gt = new String[1000];
    
    private int num;
    private ArrayList<Datanew> datalist;
	private BaseAdapter dataAdapter;
	private ListView dataListView;
	private int count1;
	private static final int option1=1,option2=2;
	int flag=0;
	private RadioButton myOption1;
	private RadioButton myOption2;
	private RadioButton myOption3;
	private Button okoption1;
	private String[] temp;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.enqexeclist);
        setTitleFromActivityLabel (R.id.title_text);
        
        Log.d("testing","before examine file");
        gd.examineXMLFile(this);
        dt =gd.enq_id;
        tt =gd.enq_name;
        gt=gd.enq_val;
        
        Log.d("testing","after examine file "+gd.ctr);
        num=gd.ctr;
        mDbHelper = new DatabaseAdapternew(this);
        mDbHelper.open();
        
        
        for(int j=0;j<num;j++)
        {
        	mDbHelper.createItem(dt[j],tt[j],gt[j]);
        }
        
        System.out.println("between");
        
		display();
		
		mDbHelper.close();
		
		SortAsc = (ImageButton)findViewById(R.id.sortas1);
		SortAsc.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sort();
			}
		});
		
		SortDesc = (ImageButton)findViewById(R.id.sortde1);
		SortDesc.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sort();
			}
		});
		
		SearchItem = (ImageButton)findViewById(R.id.search1);		
		SearchItem.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				search();
			}
		});
		
		publish = (ImageButton)findViewById(R.id.publishicon);
		publish.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				WritingPdf wp = new WritingPdf();
				wp.writing(allName, allId, allVal);
				Toast.makeText(getApplicationContext(), "Inquiry published", Toast.LENGTH_LONG).show();
			}
		});
		
		dataListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				//selectdialog(arg3);
			}
		});
        
	
    }
    
    private void search(){
			
						
    	final Dialog dialog = new Dialog(this);

    	dialog.setContentView(R.layout.searchdialog);
    	dialog.setTitle("Search Inquiry");
    	dialog.show();
    	
    	SearchText = (EditText) dialog.findViewById(R.id.searchtext);
    	okoption1 = (Button) dialog.findViewById(R.id.ok1);
    	
			
			flag=1;
			
			okoption1.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
			
			 mDbHelper.open();
			 Cursor cursor = mDbHelper.getItem(key);
			 startManagingCursor(cursor);
			 resName = new String[1000];
			 resId = new String[1000];
			 resVal = new String[1000];
			 temp=new String[1000];
			 //resPrice = new String[1000];
			 
			 if (cursor.moveToFirst()){
			   do{
				   //System.out.println("inside");
				   resName[count1] = cursor.getString(cursor.getColumnIndex("stockname"));
				   resId[count1] = cursor.getString(cursor.getColumnIndex("stockid"));
				   resId[count1] = cursor.getString(cursor.getColumnIndex("stockvalue"));
				   //resPrice[count1] = cursor.getString(cursor.getColumnIndex("stockprice"));
				   count1++;
			   
			   }while(cursor.moveToNext());
			   
			}
			
				}
			});
				dialog.dismiss();
				mDbHelper.close();
			dataListView = (ListView) findViewById(R.id.datalist1);
	        
	        //datalist = new ArrayList<Datanew>();
	        //dataAdapter = new DataViewAdapternew(getApplicationContext(), datalist);

	        for(int i=0; i<count1; i++)
	        {
	        	/*String name= resName[i];
	        	String id1 = resId[i]; 
	        	String value = resVal[i];
	        	Datanew team1 = new Datanew(name,id1,value);
	        	datalist.add(team1);
*/
	        temp[i]=resName[i]+"\t"+resId[i]+"\t"+resVal[i];	
	        }
	        
	        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
	    			android.R.layout.simple_list_item_1, temp);
	    	dataListView.setAdapter(adapter);
	    	adapter.notifyDataSetChanged();			
			
      }
    
    
    
    
    private void display(){
    	
    	
    	cursor = mDbHelper.getAllItems();
		startManagingCursor(cursor);
		flag=0;
    	allName = new String[1000];
		allId = new String[1000];
		allVal = new String[1000];
		temp = new String[1000];
		int count=0;
		
		do{
		    /*allAddress[count]= cursor.getString(1);
		    allDesc[count]= cursor.getString(2);
		    allPrice[count]= cursor.getInt(3);
		    */
			allName[count]= cursor.getString(1);
			allId[count]= cursor.getString(2);
			allVal[count]=cursor.getString(3);
			
			count++;
			
			
	
		}while(cursor.moveToNext());
		System.out.println("Display=" + count);
		
    	dataListView = (ListView) findViewById(R.id.datalist1);
        
        //this.datalist = new ArrayList<Datanew>();
        //this.dataAdapter = new DataViewAdapternew(this, datalist);

        for(int i=0; i<count; i++)
        {
        	//System.out.println("Name=" + allName[i]);
        	//System.out.println("Text=" + allId[i]);
        	
        	/*String Name= allName[i];
        	String text1 = allId[i];
        	String text2 = allVal[i];
        	        	
        	Datanew team1 = new Datanew(Name,text1,text2);
        	datalist.add(team1);*/
        	temp[i]=allName[i]+"\t"+allId[i]+"\t"+allVal[i];
        	
        }
        
              
        /*dataListView.setAdapter(dataAdapter);
        dataAdapter.notifyDataSetChanged();*/
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
    			android.R.layout.simple_list_item_1, temp);
    	dataListView.setAdapter(adapter);
    	adapter.notifyDataSetChanged();
                   
    }
    
    
    
    
    private void sort(){
    	System.out.println("bottom1");
    	int count=0;
    	flag=2;
    	//mDbHelper_sort = new DatabaseAdapter(this);
        mDbHelper.open();
    	
    	s_cursor = mDbHelper.sortItem();
    	System.out.println("bottom2");
		startManagingCursor(s_cursor);
		System.out.println("bottom3");
		
    	sort_Name = new String[1000];
		sort_Id = new String[1000];
		sort_Val = new String[1000];
		temp = new String[1000];
		
		do{
		    /*allAddress[count]= cursor.getString(1);
		    allDesc[count]= cursor.getString(2);
		    allPrice[count]= cursor.getInt(3);
		    */
			System.out.println("sortName=" + sort_Name[count]);
			sort_Name[count]= s_cursor.getString(1);
			sort_Id[count]= s_cursor.getString(2);
			sort_Val[count] = s_cursor.getString(3);
			
			count++;
		}while(s_cursor.moveToNext());
    	
		System.out.println("sort=" + count);
		
    	dataListView = (ListView) findViewById(R.id.datalist1);
        
        //this.datalist = new ArrayList<Datanew>();
        //this.dataAdapter = new DataViewAdapternew(this, datalist);

        for(int i=0; i<count; i++)
        {
        	/*System.out.println("Name=" + sort_Name[i]);
        	System.out.println("Text=" + sort_Id[i]);
        	String Name= sort_Name[i];
        	String text1 = sort_Id[i];
        	String text2 = sort_Val[i];
        	
        	Datanew team1 = new Datanew(Name,text1,text2);
        	datalist.add(team1);*/
        	temp[i]=sort_Name[i]+"\t"+sort_Id[i]+"\t"+sort_Val[i];
        	
        }
        
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
    			android.R.layout.simple_list_item_1, temp);
    	dataListView.setAdapter(adapter);
    	adapter.notifyDataSetChanged();

        mDbHelper.close();
        
    }
	/*
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        menu.add(0,option1,option1,"Preferences");
        menu.add(0,option2,option2,"Exit");
        return super.onCreateOptionsMenu(menu);
        }

        
        public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        	switch (item.getItemId()) {
            case option1:
            	Intent intent1 = new Intent(this,StockPreferences.class);
        		startActivityForResult(intent1, 0);
            	return true;
            	
            case option2:
            	return true;
            
           } 	
            	
        return false;
        }
*//*
        void selectdialog(long arg)
        {
        	Dialog dialog = new Dialog(this);

        	dialog.setContentView(R.layout.custom_dialog);
        	dialog.setTitle("Transaction");
        	        	        	
        	dialog.show();
        	
        	myOption1 = (RadioButton)findViewById(R.id.option1);
            myOption2 = (RadioButton)findViewById(R.id.option2);
            myOption3 = (RadioButton)findViewById(R.id.option3);
            
            myOption1.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					execute();
					
				}
			});
        	
		}
*/
	protected void execute() {
		// TODO Auto-generated method stub
		
		
	}
	protected void publish() {
		// TODO Auto-generated method stub
		
	}
	protected void addtofav() {
		// TODO Auto-generated method stub
		
	}
}